require.config({baseUrl: '/js/lib'});
require(['zepto', 'lib','uploadImg'], function ($, lib) {
    var fn = {}, lib = new lib();
    $.extend(fn, {
        init: function () {
            fn.getMemberInfo();
            fn.selectArea();
            fn.setMemberInfo();
            fn.uploadPic();
        },
        getMemberInfo: function () {
            lib.ajx(lib.getReq().ser + 'member/getMemberInfo.action', {}, function (data) {
                    if (data.infocode == '0') {
                        var info = data.info;
                        info.headPic ? $('#headPic').attr('src', lib.getReq().imgPath + info.headPic) : '';
                        fn.headPic = info.headPic;
                        $('#nickname').val(info.nickname);
                        $('#address').val(info.address);
                        $('#sex option[value="' + info.sex + '"]').attr("selected", true);
                        info.birthday?$('#birthday').val(info.birthday):$('#birthday').val('1990-01-01');
                        fn.getAreainfo('provinceName','',function(){
                            info.provinceAreaId?$('#provinceName').val(info.provinceAreaId):$('#provinceName').val('请选择')
                        });
                        if(info.cityAreaId){
                            fn.getAreainfo('cityName',info.provinceAreaId,function(){
                                $('#cityName').val(info.cityAreaId);
                            });
                            fn.getAreainfo('countyName',info.cityAreaId,function(){
                                $('#countyName').val(info.countyAreaId);
                            });
                        }
                    } else if (data.infocode == '2') {
                        location.href = "/login.html?backUrl=" + lib.getBackUrl();
                    } else {
                        alert('服务器开小差了，请稍后再试。')
                    }
                },
                function () {
                    console.log('collect.js => collectionList.action fail!');
                })
        },
        getAreainfo: function (type, id,callback) {
            var param = id ? {areaId: id} : '';
            lib.ajx(lib.getReq().ser + 'chinaArea/getAreaSubset.action', param, function (data) {
                    if (data.infocode == '0') {
                        var info = data.info;
                        var optionHtml = '';
                        for (var i in info) {
                            optionHtml += '<option value="' + info[i].chinaAreaId + '">' + info[i].areaName + '</option>'
                        }
                        $('#' + type).append(optionHtml);
                        if(typeof callback === 'function') callback.call();
                    } else {
                        alert('服务器开小差了，请稍后再试。')
                    }
                },
                function () {
                    console.log('collect.js => collectionList.action fail!');
                })
        },
        selectArea: function () {
            $('#provinceName,#cityName,#countyName').off().on('change', function () {
                if ($(this).attr('id') === 'provinceName') {
                    $('#cityName,#countyName').html('<option>请选择</option>');
                    fn.getAreainfo('cityName', $(this).val())
                } else if ($(this).attr('id') === 'cityName') {
                    $('#countyName').html('<option>请选择</option>');
                    fn.getAreainfo('countyName', $(this).val())
                }
            })
        },
        setMemberInfo: function () {
            $('#savaBtn').off().on('tap', function () {
                var param = {
                    'headUrl': fn.headPic,
                    'nickname': $('#nickname').val(),
                    'areaId': $('#countyName').val() !== '请选择' ? $('#countyName').val() : '',
                    'address': $('#address').val(),
                    'sex': $('#sex').val(),
                    'birthday': $('#birthday').val()
                };
                lib.ajx(lib.getReq().ser + 'member/setMemberInfo.action', param, function (data) {
                        if (data.infocode == '0') {
                            alert('保存成功！');
                        } else if (data.infocode == '2') {
                            location.href = "/login.html?backUrl=" + lib.getBackUrl();
                        } else {
                            alert('服务器开小差了，请稍后再试。')
                        }
                    },
                    function () {
                        console.log('collect.js => collectionList.action fail!');
                })
            })
        },
        uploadPic: function(){
            $('#upload_pic').off().on('tap',function(){
                $('.image-cropper-bg').show();
            })
        }
    });
    fn.init();
});